//
//  HSYBaseModel.m
//  高仿暴走斗图
//
//  Created by huangsongyao on 16/3/4.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "HSYBaseModel.h"
#import "RACSignal+Timer.h"
#import "NSArray+RAC.h"
#import "NSDictionary+RAC.h"
#import "NSArray+Filter.h"

@interface HSYBaseModel ()

@property (nonatomic, strong, readonly) RACDisposable *disposable;      //计时器

@end

@implementation HSYBaseModel

- (instancetype)init {
    if (self = [super init]) {
        
        self.datas = [[NSMutableArray alloc] init];
    }
    return self;
}


#pragma mark - Button RACCommand For RACSignal

- (RACCommand *)createCommandWithSignal:(RACSignal *)signal {
    
    RACCommand *command = [[RACCommand alloc] initWithEnabled:signal signalBlock:^RACSignal *(id input) {
        
        return [RACSignal empty];
        
    }];
    
    return command;
}

- (RACSignal *)createRACSignals:(id<NSFastEnumeration>)signals reduce:(id (^)())reduceBlock {
    
    return [RACSignal combineLatest:signals reduce:reduceBlock];
    
}

#pragma mark - Array && Dictionary For RAC

+ (void)rac_traverseArray:(NSArray *)array nextBlock:(void(^)(id x, NSInteger index))block completed:(void(^)(void))completed {
    
    [[[HSYBaseModel rac_traverseArray:array] deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(NSDictionary *dic) {
        
        block(dic, [[dic.allKeys firstObject] integerValue]);
    } completed:^{
        completed();
    }];
}

- (void)rac_traverseArray:(NSArray *)array nextBlock:(void(^)(id x, NSInteger index))block completed:(void(^)(void))completed {
    
    [HSYBaseModel rac_traverseArray:array nextBlock:block completed:completed];
}

+ (RACSignal *)rac_traverseArray:(NSArray *)array {
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [[[array rac_traverseArray] deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(id x) {
            [subscriber sendNext:x];
        } completed:^{
            [subscriber sendCompleted];
        }];
        return nil;
    }];
}

+ (void)rac_traverseDictionary:(NSDictionary *)dictionary nextBlock:(void(^)(NSString *key, NSString *value))block completed:(void(^)(void))completed {
    
    [[[HSYBaseModel rac_traverseDictionary:dictionary] deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(NSDictionary *dic) {
        
        block([dic.allKeys firstObject], [dic.allValues firstObject]);
    } completed:^{
        completed();
    }];
}

- (void)rac_traverseDictionary:(NSDictionary *)dictionary nextBlock:(void(^)(NSString *key, NSString *value))block completed:(void(^)(void))completed {
    
    [HSYBaseModel rac_traverseDictionary:dictionary nextBlock:block completed:completed];
}

+ (RACSignal *)rac_traverseDictionary:(NSDictionary *)dictionary {
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [dictionary rac_traverseDictionaryForSubscribeNext:^(id key, id value) {
            [subscriber sendNext:@{
                                   key : value,
                                   }];
        } traverseCompleted:^{
            [subscriber sendCompleted];
        }];
        return nil;
    }];
}

+ (RACSequence *)rac_traverseArray:(NSArray *)array filerBlock:(BOOL(^)(id x))flier mapBlock:(id(^)(id x))map
{
    RACSequence *results = [array rac_traverseFilerBlock:^BOOL(id x) {
        if (flier) {
            return flier(x);
        }
        return NO;
    } mapBlock:^id(id x) {
        if (map) {
            return map(map);
        }
        return nil;
    }];
    return results;
}

- (RACSequence *)rac_traverseArray:(NSArray *)array filerBlock:(BOOL(^)(id x))flier mapBlock:(id(^)(id x))map
{
    return [self.class rac_traverseArray:array filerBlock:flier mapBlock:map];
}

#pragma mark - Observe Object

- (void)rac_ignoreObject:(id)object observeSubscribeNext:(void(^)(id value))next
{
    [[[RACObserve(self, self.objectPoint) filter:^BOOL(id value) {
        if (object && [object isEqual:value]) {
            //如果一致时，吞噬掉信号，不触发监听事件
            return NO;
        }
        //否则发送信号向上传递
        return YES;
    }] deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(id value) {
        //当获取到信号时被触发
        if (next) {
            next(value);
        }
    }];
}

- (void)rac_takeUntilCompleted:(BOOL(^)(id predicate))completed toMap:(id(^)(id value))map observeSubscribeNext:(void(^)(NSNumber *value))next
{
    [[[[RACObserve(self, self.objectPoint) takeUntilBlock:^BOOL(id x) {
        if (completed) {
            return completed(x);
        }
        return NO;
    }] map:^id(id value) {
        return map(value);
    }] deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(NSNumber *x) {
        if (next) {
            next(x);
        }
    }];
}

- (RACSignal *)rac_filterUntilCompleted:(BOOL(^)(id predicate))completed toMap:(id(^)(id value))map
{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [[[self.datas rac_filterUntilCompleted:completed toMap:map] deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(id x) {
            [subscriber sendNext:x];
        } completed:^{
            [subscriber sendCompleted];
        }];
        return [RACDisposable disposableWithBlock:^{
            
        }];
    }];
}

#pragma mark - Signal Mix Operation

- (RACSignal *)rac_zipSignals:(NSArray <RACSignal *>*)signals reduce:(id(^)())reduce
{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [[[RACSignal zip:signals reduce:reduce] deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(id result) {
            [subscriber sendNext:result];
        } completed:^{
            [subscriber sendCompleted];
        }];
        return [RACDisposable disposableWithBlock:^{}];
    }];
}

- (RACSignal *)rac_zipWithSignal:(RACSignal *)signal ofOtherSignal:(RACSignal *)otherSignal
{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [[[signal zipWith:otherSignal] deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(id x) {
            [subscriber sendNext:x];
        } completed:^{
            [subscriber sendCompleted];
        }];
        return [RACDisposable disposableWithBlock:^{}];
    }];
}

- (RACSignal *)rac_concatSignal:(RACSignal *)signal ofOtherSignal:(RACSignal *)otherSignal
{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [[[signal concat:otherSignal] deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(id x) {
            [subscriber sendNext:x];
        } completed:^{
            [subscriber sendCompleted];
        }];
        return [RACDisposable disposableWithBlock:^{
        }];
    }];
}

#pragma mark - RAC Timer

- (void)rac_starTimerWithNextBlock:(void(^)(id x))block {
    
    _disposable = [[[RACSignal timerSignalOneMinute] deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(id x) {
        block(x);
    }];
}


- (void)rac_stopTimer {
    
    if (self.disposable) {
        [self.disposable dispose];
        _disposable = nil;
    }
}


#pragma mark - ShareSDK

- (RACSignal *)shareInView:(UIView *)view content:(NSString *)content defaultContent:(NSString *)defaultContent image:(NSString *)image title:(NSString *)title url:(NSString *)url description:(NSString *)description shareList:(NSArray *)shareList {
    
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
       
//        NSString * imagePath = [[NSBundle mainBundle] pathForResource:image ofType:@"png"];
//        //构造分享内容
//        id<ISSContent> publishContent = [ShareSDK content:content
//                                           defaultContent:defaultContent
//                                                    image:[ShareSDK imageWithPath:imagePath]
//                                                    title:title
//                                                      url:url
//                                              description:description
//                                                mediaType:mediaType];
//        //创建弹出菜单容器
//        id<ISSContainer> container = [ShareSDK container];
//        [container setIPadContainerWithView:view arrowDirect:UIPopoverArrowDirectionUp];
//        
//        //弹出分享菜单
//        [ShareSDK showShareActionSheet:container
//                             shareList:shareList //为nil时，自动显示所有包中的分享
//                               content:publishContent
//                         statusBarTips:YES
//                           authOptions:nil
//                          shareOptions:nil
//                                result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
//                                    
//                                    if (state == SSResponseStateSuccess) {
//                                        
//                                        NSLog(NSLocalizedString(@"TEXT_ShARE_SUC", @"分享成功"));
//                                        [subscriber sendNext:@(kShareStatusTypeSuccess)];
//                                        [subscriber sendCompleted]
//                                        
//                                    } else if (state == SSResponseStateFail) {
//                                        
//                                        NSLog(NSLocalizedString(@"TEXT_ShARE_FAI", @"分享失败,错误码:%d,错误描述:%@"), [error errorCode], [error errorDescription]);
//                                        [subscriber sendError:(NSError *)error];
//
//                                    }
//                                }];

        return [RACDisposable disposableWithBlock:^{
            
        }];
    }];
}

@end
